//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _PRIMITIVE_H
#define _PRIMITIVE_H
//
#include <global.h>
#include <common/restoreerror_fwd.h>
#include <common/restorestream.h>
#include "uierror.h"
//
class Document;
class QDataStream;
class Primitive
{
//types
public:
   typedef Primitive* (*CreatorFunction)(unsigned int type, RestoreStream &p_stream);
   enum State
   {
      incomplete,
      before_first,
      before,
      after
   };
//disabled
private:
   Primitive(const Primitive&);
   Primitive &operator=(const Primitive&);
//construction
public:
   Primitive(unsigned int p_type);
   static Primitive *create(RestoreStream &p_stream);
   virtual ~Primitive();
//usage
public:
   UiError exec(Document *p_doc);
   UiError undo(Document *p_doc);
//serialization
public:
   void store_data(QDataStream &p_stream) const;
   void restore_data(RestoreStream &p_stream);
//for implementers
public:
   static void install_creator_function(unsigned int p_type,CreatorFunction p_f);
   State get_state() const;
protected:
   void prepare_base(const Document *p_doc);
   virtual UiError exec_impl(Document *p_doc)=0;
   virtual UiError undo_impl(Document *p_doc)=0;
   virtual void store_data_impl(QDataStream &p_stream) const;
   virtual void restore_data_impl(RestoreStream &p_stream);
//data
public:
   unsigned int const type;
private:
   State state;
//data access
public:
   bool was_undo() const
      {return state!=after;}
   bool about_to_undo() const
      {return state==after;}
};
//
class PrimitiveDelegate
{
//types
public:
   typedef PrimitiveDelegate* (*CreatorFunction)(unsigned int p_type,RestoreStream &p_stream);
//disabled
private:
   PrimitiveDelegate(const PrimitiveDelegate&);
   PrimitiveDelegate &operator=(const PrimitiveDelegate&);
//construction
public:
   PrimitiveDelegate(unsigned int p_type);
   static PrimitiveDelegate *create(RestoreStream &p_stream) ;
   virtual ~PrimitiveDelegate();
//serialization
public:
   void store_data(QDataStream &p_stream) const;
   void restore_data(RestoreStream &p_stream);
//for implementers
public:
   static void install_creator_function(unsigned int p_type,CreatorFunction p_f);
   inline unsigned int get_type() const {return type;}
protected:
   virtual void store_data_impl(QDataStream &p_stream) const;
   virtual void restore_data_impl(RestoreStream &p_stream);
//data
private:
   const unsigned int type;
};
//
#endif
